package com.health.form;

import java.util.Date;

import org.apache.struts.action.ActionForm;

import com.health.model.User;

/**
 * UserEntity include "permission","status"
 */
public class UserForm extends ActionForm
{
	/**
	 * default serializable ID
	 */
	private static final long	serialVersionUID	= 1L;

	private String				id;
	private String				name;
	private String				password;
	private String				email;						// unique
	private String				gender;					// 0:male 1:female
	private Date				birthdate;					//
	private Date				registertime;				// exact to second
	private String				permission;				// 0:admin 1:normal user
	private String				status;					// 0:normal 1:forbidden
	private Integer				loginCount;				// how much times login
	// failed, to record the
	// times
	private Date				startBirthdate;			// time scope fro
	// query
	private Date				endBirthdate;				// time scope fro
	// query

	// be used to paging
	private Integer				start;
	private Integer				limit;

	/**
	 * constructor
	 */
	public UserForm()
	{}

	public UserForm(String id, String name, String password, String gender,
			String email, Date birthdate, Date registertime, String permission,
			String status)
	{
		this.id = id;
		this.name = name;
		this.password = password;
		this.gender = gender;
		this.email = email;
		this.birthdate = birthdate;
		this.registertime = registertime;
		this.permission = permission;
		this.status = status;
	}

	/***************************************************************************
	 * convert form object to model object, because some properties type are
	 * different
	 **************************************************************************/
	public User convertFormToModel(UserForm form)
	{
		String genderStr = form.getGender();
		String permissionStr = form.getPermission();
		String statusStr = form.getStatus();
		Integer gender = null;
		Integer permission = null;
		Integer status = null;
		if (null != genderStr && !"".equals(genderStr))
		{
			gender = Integer.parseInt(genderStr);
		}
		else
		{
			gender = null;
		}
		if (null != permissionStr && !"".equals(permissionStr))
		{
			permission = Integer.parseInt(permissionStr);
		}
		else
		{
			permission = null;
		}
		if (null != statusStr && !"".equals(statusStr))
		{
			status = Integer.parseInt(statusStr);
		}
		else
		{
			status = null;
		}
		User user = new User();
		user.setId(form.getId());
		user.setName(form.getName());
		user.setPassword(form.getPassword());
		user.setEmail(form.getEmail());
		user.setBirthdate(form.getBirthdate());
		user.setStartBirthdate(form.getStartBirthdate());
		user.setEndBirthdate(form.getEndBirthdate());
		user.setRegistertime(form.getRegistertime());
		user.setStart(form.getStart());
		user.setLimit(form.getLimit());

		user.setGender(gender);
		user.setStatus(status);
		user.setPermission(permission);

		return user;
	}

	@Override
	public String toString()
	{
		return this.id + " " + name + " " + password + " " + gender + " "
				+ birthdate.toString() + " " + registertime.toString()
				+ permission + " " + status;
	}

	/**
	 * getters and setters
	 */
	public String getId()
	{
		return id;
	}

	public void setId(String id)
	{
		this.id = id;
	}

	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public String getPassword()
	{
		return password;
	}

	public void setPassword(String password)
	{
		this.password = password;
	}

	public String getEmail()
	{
		return email;
	}

	public void setEmail(String email)
	{
		this.email = email;
	}

	public String getGender()
	{
		return gender;
	}

	public void setGender(String gender)
	{
		this.gender = gender;
	}

	public Date getBirthdate()
	{
		return birthdate;
	}

	public void setBirthdate(Date birthdate)
	{
		this.birthdate = birthdate;
	}

	public Date getRegistertime()
	{
		return registertime;
	}

	public void setRegistertime(Date registertime)
	{
		this.registertime = registertime;
	}

	public String getPermission()
	{
		return permission;
	}

	public void setPermission(String permission)
	{
		this.permission = permission;
	}

	public String getStatus()
	{
		return status;
	}

	public void setStatus(String status)
	{
		this.status = status;
	}

	public Date getStartBirthdate()
	{
		return startBirthdate;
	}

	public void setStartBirthdate(Date startBirthdate)
	{
		this.startBirthdate = startBirthdate;
	}

	public Date getEndBirthdate()
	{
		return endBirthdate;
	}

	public void setEndBirthdate(Date endBirthdate)
	{
		this.endBirthdate = endBirthdate;
	}

	public Integer getStart()
	{
		return start;
	}

	public void setStart(Integer start)
	{
		this.start = start;
	}

	public Integer getLimit()
	{
		return limit;
	}

	public void setLimit(Integer limit)
	{
		this.limit = limit;
	}

	public Integer getLoginCount()
	{
		return loginCount;
	}

	public void setLoginCount(Integer loginCount)
	{
		this.loginCount = loginCount;
	}

}
